<!DOCTYPE html>
<html>
<head>
	<title>引流宝 - 版本升级</title>
	<meta charset="utf-8">
	<link rel="stylesheet" href="../install.css">
	<link rel="shortcut icon" href="../../../static/img/favicon.png">
	<script src="../../static/js/jquery.min.js"></script>
</head>
<body>
    <div id="app" style="display:block;">
        <div class="container" style="margin-top: 100px;">
            <div class="logo"></div>
            <div class="form-view">
                <p class="success-text"> 检测中... </p>
                <div id="progress-bar" onclick="upgrade()" style="display:none;">
                    <div id="progress-fill"></div>
                    <div id="progress-num">立即升级</div>
                </div>
            </div>
            <p class="tips"></p>
        </div>
    </div>
    
    <script>
    
        // 检测脚本
        fetch('upgradeCheck.php')
        .then(response => {
            return response.json();
        })
        .then(res => {
            
            // 检测结果
            if(res.code == 200) {
                document.querySelector('.success-text').innerHTML = res.msg;
                $('#progress-bar').css('display','block');
            }else {
                document.querySelector('.success-text').innerHTML = res.msg;
            }
        })
        .catch(error => {
            const buttonView = document.querySelector('.success-text');
            buttonView.innerHTML = '<p style="text-align:center;margin-top:30px;color:#f00;font-size:15px;">升级环境异常，请检查你建立的网站是否选择了php环境。</p><a href="../../static/img/errorIMG.png" target="_blank" style="display:block;text-align:center;text-decoration:underline;margin-top:10px;font-size:15px;">点击查看解决方法~</a>';
        });
        
        function upgrade() {
            
            // 获取按钮、进度条、进度数值
            const progressBar = document.getElementById("progress-bar");
            const fillProgress = document.getElementById("progress-fill");
            const ProgressNum = document.getElementById("progress-num");
            
            // 初始化
            let progress = 0;
            const totalDuration = 3000;
            const intervalDuration = 50;
            
            $.ajax({
                type: "POST",
                url: "upgrade.php",
                success: function(res){
                    
                    // 成功
                    if(res.code == 200){
                        
                        const updateProgressBar = () => {
                
                        progress += (intervalDuration / totalDuration) * 100;
                        if (progress >= 100) {
                            progress = 100;
                            clearInterval(progressInterval);
                        }
                        
                        // 修改状态
                        fillProgress.style.width = `${progress}%`;
                        ProgressNum.innerText = `升级中 ${Math.floor(progress)}%`;
                        
                        // 当进度到30的时候修改文字颜色
                        if(progress >= 30) {
                            ProgressNum.style.color = '#fff';
                        }
                        
                        // 当进度到100的时候修改文字
                        if(progress == 100) {
                            ProgressNum.innerText = `升级完成`;
                            
                            // 跳转到安装成功的页面
                            setTimeout(function(){
                                location.href = '../../console/index';
                            },800)
                        }
                    };
                    
                    const progressInterval = setInterval(updateProgressBar, intervalDuration);
                    }else{
                        
                        // 失败
                        $('.tips').html('<span style="color:#f00;">'+res.msg+'</span>')
                    }
                },
                error: function() {
                    
                    // 服务器发生错误
                    alert('upgrade.php发生错误~')
                }
            });
        }
        
        function yinliubao_install_record(current_time,client_ip,server_ip,user_name,user_email) {
            $.ajax({
                type: "POST",
                url: 'https://likeyunkeji.likeyunba.com/yinliubao-install-record/?time=' + current_time + '&type=安装&client_ip=' + client_ip + '&server_ip=' + server_ip + '&user_name=' + user_name + '&user_email=' + user_email,
                success: function(res){
                    console.log(res)
                }
            });
        }
    </script>
</body>
</html>